package com.lvyq.javaprint.view.tray;

 import com.lvyq.javaprint.constant.ImgConstant;
 import com.lvyq.javaprint.constant.TrayConstant;
 import com.lvyq.javaprint.util.SettingUtils;
 import com.lvyq.javaprint.view.style.STYLE;
 import javafx.application.Platform;
 import javafx.event.EventHandler;
 import javafx.geometry.Pos;
 import javafx.scene.control.Button;
 import javafx.scene.image.Image;
 import javafx.scene.image.ImageView;
 import javafx.scene.input.MouseButton;
 import javafx.scene.input.MouseEvent;
 import javafx.scene.layout.BorderPane;
 import javafx.scene.layout.HBox;
 import javafx.scene.layout.VBox;
 import javafx.scene.shape.Line;
 import javafx.stage.Stage;
 import java.io.IOException;


/**
 * @author lvyq
 * @version 1.0
 * Create by 2024-04-28 10:09
 */
public class TrayButtonView {

    /**
     * @description  创建操作菜单
     * @author lvyq
     * @throws
     * @return VBox 
     * @time 2024-04-29 11:15
     */
    public static BorderPane createOperateMenu(Stage trayStage) {
        BorderPane borderPane = new BorderPane();


        borderPane.getChildren().add(SettingUtils.getBackGroundImg()  );
        //中间操作
        borderPane.setCenter(createCenterView(trayStage));
        return borderPane;
    }

    /**
     * @description 创建中间操作
     * @author lvyq
     * @throws
     * @return Node
     * @time 2024-04-29 14:14
     */
    private static VBox createCenterView(Stage trayStage ) {
        VBox vBox = new VBox(2);
        Button helpBut = CenterView.createHelpBut();
        Button versionBut = CenterView.createVersionBut();
        Button authorBut = new Button(TrayConstant.AUTHOR);
        authorBut.setPrefWidth(TrayConstant.TRAY_STAGE_WIDTH);
        authorBut.setAlignment(Pos.BASELINE_LEFT);
        authorBut.setGraphic(new ImageView(new Image(ImgConstant.TRAY_AUTHOR)));

        STYLE.BUTTON.toggle_style(authorBut);

        //开机自启
        HBox selfStartBox = null;
        try {
            selfStartBox = CenterView.createSelfStartBut();
        } catch (IOException |RuntimeException|InterruptedException e) {
            e.printStackTrace();
        }


        //设置字体
        vBox.setStyle("-fx-font-size: 13px;-fx-background-color:rgba(0.0,0.0,0.0,0.0)");
        // 添加分割线
        Line line = new Line();
        line.setStartX(0);
        line.setEndX(180); // 设置分割线的长度
        line.getStyleClass().add("line");

        //退出按钮
        Button exitBut = createExitBut(trayStage);

        vBox.getChildren().addAll(selfStartBox,line,helpBut,versionBut,authorBut,exitBut);

        return vBox;
    }


    /**
     * @description 退出按钮
     * @author lvyq
     * @throws
     * @return Button
     * @time 2024-04-29 12:08
     */
    public static Button createExitBut(Stage trayStage) {
        Button button = new Button("退出");
        button.setPrefWidth(TrayConstant.TRAY_STAGE_WIDTH);
        button.setAlignment(Pos.BASELINE_LEFT);
        button.setGraphic(new ImageView(new Image(ImgConstant.EXIT)));
        STYLE.BUTTON.toggle_style(button);
        button.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if (mouseEvent.getButton()== MouseButton.PRIMARY){
                    //关闭托盘窗口
                    trayStage.close();
                    //退出程序
                    Platform.exit();
                    System.exit(0);
                }
            }
        });
        return button;
    }
}
